package com.cloudxingkong.dianyinke.Student.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Ui.AMapForStudent;
import com.cloudxingkong.dianyinke.Student.bean.FuJinLaoShi;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ningfei on 2016/3/26.
 */
public class FuJinLaoShiAdapter extends BaseAdapter {
	private List<FuJinLaoShi.ListBean> data;
	private Context context;
	private onTouchItemPosition onTouchPosition;

	public FuJinLaoShiAdapter (List<FuJinLaoShi.ListBean> data, Context context, onTouchItemPosition onTouchPosition) {
		this.data = data;
		this.context = context;
		this.onTouchPosition = onTouchPosition;
	}

	@Override
	public int getCount () {
		return data == null ? 0 : data.size ();
	}

	@Override
	public Object getItem (int position) {
		return data == null ? null : data.get (position);
	}

	@Override
	public long getItemId (int position) {
		return position;
	}

	@Override
	public View getView (int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate (context, R.layout.fujinlaoshi_item, null);
			holder = new ViewHolder ();
			holder.tv_fjls_name = (TextView) convertView.findViewById (R.id.tv_fjls_name);
			holder.tv_fjls_title = (TextView) convertView.findViewById (R.id.tv_fjls_title);
			holder.tv_fjls_content = (TextView) convertView.findViewById (R.id.tv_fjls_content);
			holder.tv_fjls_price = (TextView) convertView.findViewById (R.id.tv_fjls_price);
			holder.tv_fjls_distance = (TextView) convertView.findViewById (R.id.tv_fjls_distance);
			holder.ll_distance = (LinearLayout) convertView.findViewById (R.id.ll_distance);

			holder.wwv = ((WordWrapView) convertView.findViewById (R.id.fjls_view));
			//老师头像
			holder.fjls_icon = (CircleImageView) convertView.findViewById (R.id.fjls_icon);

			convertView.setTag (holder);
		} else {
			holder = (ViewHolder) convertView.getTag ();
		}
		onTouchPosition.onTouch (position);

		if (data.get (position).getMemname () != null) {
			holder.tv_fjls_name.setText (data.get (position).getMemname () + "");//老师名字
		}
		holder.tv_fjls_title.setText (data.get (position).getTitle () + "");//标题
		holder.tv_fjls_content.setText (data.get (position).getNote () + "");//内容
		holder.tv_fjls_price.setText ("￥" + data.get (position).getPrice () + "");//价格
		//距离多少米
		double dsitance = data.get (position).getDistance ();
		if (dsitance < 1000) {
			String format = String.format ("%.2f", dsitance);

			holder.tv_fjls_distance.setText (format + "m");

		} else if (dsitance >= 1000) {
			String format = String.format ("%.2f", dsitance / 1000);

			holder.tv_fjls_distance.setText (format + "km");
		}

		String avatar = data.get (position).getCoverpic ();
		if (avatar != null && !avatar.equals ("")) {
			ImageLoader.getInstance ().displayImage (avatar, holder.fjls_icon);
		}

//        ImageLoader.getInstance().displayImage(data.get(position).getAvatar(),holder.fjls_icon);

		String label = data.get (position).getLabel ();//标签
		if (label != null) {
			String[] split = label.split (",");
			for (int i = 0; i < split.length; i++) {
				TextView tv = new TextView (context);
				tv.setGravity (Gravity.CENTER);
				tv.setTextColor (context.getResources ().getColor (R.color.main));
				tv.setBackgroundResource (R.drawable.shape_rectangle_white_solid_btn);
				tv.setText (split[i]);
				if (holder.wwv.getChildCount () < split.length) {
					holder.wwv.addView (tv);
				}
				//Log.e("test", position + " -- " + holder.wwv.getChildCount() + " -- " + split.length + " -- " + split[i] + " --- " + tv.getText());
			}
		} else {
			Log.e ("test", "标签为空");
		}

		holder.ll_distance.setOnClickListener (new View.OnClickListener () {
			@Override
			public void onClick (View v) {
				context.startActivity (new Intent (context, AMapForStudent.class));
			}
		});
		return convertView;
	}

	private class ViewHolder {
		TextView tv_fjls_name, tv_fjls_title, tv_fjls_content, tv_fjls_price, tv_fjls_distance;
		LinearLayout ll_distance;
		WordWrapView wwv;
		CircleImageView fjls_icon;

	}

	public interface onTouchItemPosition {
		public void onTouch (int position);
	}

}